<?php

//------------------------------------------------------------------------------
// Class              : ModelBase
// Modification       :
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2007.03.08         Oanhhtk                     Created 
//------------------------------------------------------------------------------
// 2007.02.06         Tran Thanh                      Update
//------------------------------------------------------------------------------
// 2008.01.21         Oanhhtk                     Update
//------------------------------------------------------------------------------
// 2013.08.13         NguyenNgocManh                  Update
//------------------------------------------------------------------------------

class ControlBase {

    //==========================================================================
    // Function    : ControlBase
    // Argument    :
    // ReturnValue :
    // Creator     : Tran Thanh 2007.06.02
    // Details     : AppendData record of table
    //==========================================================================
    function ControlBase() {
	global $db;
	$this->db = new MySql();
	$this->db = &$db;
    }

    //==========================================================================
    // Function    : execute
    // Argument    :
    // ReturnValue :
    // Creator     : Tran Thanh 2007.06.02
    // Details     : AppendData record of table
    //==========================================================================
    function execute($action) {
	echo "Method execute not define!";
    }

    //==========================================================================
    // Function    : getAll
    // Argument    :
    // ReturnValue :
    // Creator     : Tran Thanh 2007.06.02
    // Details     : AppendData record of table
    //==========================================================================
    function getAll() {
	
    }

// 2013.08.13         NguyenNgocManh                  Created
    function utf8convert($str) {

	if (!$str)
	    return false;

	$utf8 = array(
	    'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
	    'd' => 'đ|Đ',
	    'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
	    'i' => 'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
	    'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
	    'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
	    'y' => 'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
	);
	foreach ($utf8 as $ascii => $uni)
	    $str = preg_replace("/($uni)/i", $ascii, $str);
	return $str;
    }

// 2013.08.13         NguyenNgocManh                  Created
    function utf8tourl($text) {
	$text = strtolower($text);
	$text = str_replace("ß", "ss", $text);
	$text = str_replace("%", "", $text);
//        $text = preg_replace("/[^_a-zA-Z0-9 -]/", "", $text);
	$text = str_replace(array('%20', ' '), '-', $text);
	$text = str_replace("----", "-", $text);
	$text = str_replace("---", "-", $text);
	$text = str_replace("--", "-", $text);
	return $text;
    }

    //--------------------------------------------------------------------------
    //	Fucntion : DoUploadFile1()
    //  Outline  : upaload file to folder in the server
    //  Create   : NgocVQ 2007.05.02
    // Update      : NguyenNgocManh  2013.07.25
    //--------------------------------------------------------------------------
    function DoUploadFileNews($dir) {
	if ($_FILES["product_img"]['name'] != "") {
	    $ten_file_1 = time() . "_" . $_FILES["product_img"]['name'];
	    $ten_file_2 = $this->utf8convert($ten_file_1);
	    $ten_file = $this->utf8tourl($ten_file_2);
	    $_SESSION['Pkd_images1'] = $ten_file;
	    if (move_uploaded_file($_FILES["product_img"]['tmp_name'], $dir . $ten_file)) {
		$this->resize_images_new($dir, $ten_file, $ten_file, '900', '900');
	    } else {
		echo "Khong upload duoc anh. Ban kiem tra lai ten anh xem co dung khong nhe.";
	    }
	}
    }

    //--------------------------------------------------------------------------
    //	Fucntion : DoUploadFile1()
    //  Outline  : upaload file to folder in the server
    //  Create   : NgocVQ 2007.05.02
    // Update      : NguyenNgocManh  2013.07.25
    //--------------------------------------------------------------------------
    function DoUploadFileNews_Gallery($dir) {
	if ($_FILES["url_pic"]['name'] != "") {
	    $ten_anh2 = time() . "_" . $_FILES["url_pic"]['name'];
	    $ten_file_t = $this->utf8convert($ten_anh2);
	    $ten_file = $this->utf8tourl($ten_file_t);
	    $_SESSION['Pkd_images2'] = $ten_file;
	    if (move_uploaded_file($_FILES["url_pic"]['tmp_name'], $dir . $ten_file)) {
		$this->resize_images_new($dir, $ten_file, $ten_file, '900', '900');
	    } else {
		echo "Khong upload duoc anh. Ban kiem tra lai ten anh xem co dung khong nhe.";
	    }
	}
    }

    function DoUploadFileNews_ALL($dir, $field) {
	if ($_FILES[$field]['name'] != "") {
	    $ten_anh2 = time() . "_" . $_FILES[$field]['name'];
	    $ten_file_t = $this->utf8convert($ten_anh2);
	    $ten_file = $this->utf8tourl($ten_file_t);
	    $_SESSION['Pkd_images_ALL'] = $ten_file;
	    if (move_uploaded_file($_FILES[$field]['tmp_name'], $dir . $ten_file)) {
		$this->resize_images_new($dir, $ten_file, $ten_file, '900', '900');
	    } else {
		echo "Khong upload duoc anh. Ban kiem tra lai ten anh xem co dung khong nhe.";
	    }
	}
    }

    //--------------------------------------------------------------------------
    //	Fucntion : resize images
    //  Outline  : IncludeFileLang
    //  Create   : Oanhhtk 2008.01.21
    //--------------------------------------------------------------------------
    function resize_images_new($dir, $file_name_old, $file_name_new, $new_w, $new_h) {

	ini_set("memory_limit", "-1");

	$filename_original = $dir . $file_name_old;
	$filename_resized = $dir . $file_name_new;
	$new_w = "800";
	$new_h = "800";
	$extension = pathinfo($filename_original, PATHINFO_EXTENSION);

	if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG')))
	    $src_img = imagecreatefromjpeg($filename_original);

	if (in_array($extension, array('png')))
	    $src_img = imagecreatefrompng($filename_original);

	if (!$src_img)
	    return false;

	$old_w = imageSX($src_img);
	$old_h = imageSY($src_img);

	$x_ratio = $new_w / $old_w;
	$y_ratio = $new_h / $old_h;

	if (($old_w <= $new_w) && ($old_h <= $new_h)) {
	    $thumb_w = $old_w;
	    $thumb_h = $old_h;
	} elseif ($y_ratio <= $x_ratio) {
	    $thumb_w = round($old_w * $y_ratio);
	    $thumb_h = round($old_h * $y_ratio);
	} else {
	    $thumb_w = round($old_w * $x_ratio);
	    $thumb_h = round($old_h * $x_ratio);
	}

	$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
	imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_w, $old_h);

	if (preg_match("/png/", $extension))
	    imagepng($dst_img, $filename_resized);
	else
	    imagejpeg($dst_img, $filename_resized, 100);

	imagedestroy($dst_img);
	imagedestroy($src_img);
    }

    function resize_images($path_thumbs, $img_thumb_width, $field) {
	if ($_FILES[$field]['name'] != '') {
	    //make sure this directory is writable!
	    $path_thumbs = $path_thumbs;
	    //the new width of the resized image, in pixels.
	    $img_thumb_width = $img_thumb_width;
	    $extlimit = "yes";
	    $limitedext = array(".gif", ".jpg", ".png", ".jpeg", ".bmp");
	    $file_type = $_FILES[$field]['type'];
	    $file_name = $_FILES[$field]['name'];
	    $file_size = $_FILES[$field]['size'];
	    $file_tmp = $_FILES[$field]['tmp_name'];
	    //check the file's extension
	    $ext = strrchr($file_name, '.');
	    $ext = strtolower($ext);
	    if (($extlimit == "yes") && (!in_array($ext, $limitedext))) {
		echo "Wrong file extension.";
		exit();
	    }
	    //so, whats the file's extension?
	    $getExt = explode('.', $file_name);
	    $file_ext = $getExt[count($getExt) - 1];
	    //create a random file name
	    $rand_name = time() . "_" . $file_name;
	    $ThumbWidth = $img_thumb_width;
	    //keep image type
	    if ($file_size) {
		if ($file_type == "image/pjpeg" || $file_type == "image/jpeg") {
		    $new_img = imagecreatefromjpeg($file_tmp);
		} elseif ($file_type == "image/x-png" || $file_type == "image/png") {
		    $new_img = imagecreatefrompng($file_tmp);
		} elseif ($file_type == "image/gif") {
		    $new_img = imagecreatefromgif($file_tmp);
		}
		list($width, $height) = getimagesize($file_tmp);
		$imgratio = $width / $height;
		if ($imgratio > 1) {
		    $newwidth = $ThumbWidth;
		    $newheight = $ThumbWidth / $imgratio;
		} else {
		    $newheight = $ThumbWidth;
		    $newwidth = $ThumbWidth * $imgratio;
		}
		if (function_exists(imagecreatetruecolor)) {
		    $resized_img = imagecreatetruecolor($newwidth, $newheight);
		} else {
		    die("Error: Please make sure you have GD library ver 2+");
		}
		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		//ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
		ImageJpeg($resized_img, "$path_thumbs/$rand_name"); //2010.08.28.QuynhTTN
		ImageDestroy($resized_img);
		ImageDestroy($new_img);
		//$_SESSION['Pkd_images'] = $rand_name.".".$file_ext;
		$_SESSION['Pkd_images_ReSize'] = $rand_name; //2010.08.28.QuynhTTN
	    }
	}
    }

    //--------------------------------------------------------------------------
    //	Fucntion : IncludeFileLang()
    //  Outline  : IncludeFileLang
    //  Create   : Oanhhtk 2008.01.21
    //--------------------------------------------------------------------------
    function includeFileLang($fileName) {
	// check exits
	// Include once
	//echo LANG_DIR.$fileName;
	include_once(LANG_DIR . $fileName);
    }

    function upload_Multifile($field_name, $dirUpload) {
	$attach = $_FILES[$field_name];
	$array_t = array();
	for ($i = 0; $i < count($_FILES[$field_name]['name']); $i++) {
	    $ten_anh = time() . "_" . $attach['name'][$i];
	    $ten_file_t = $this->utf8convert($ten_anh);
	    $ten_file = $this->utf8tourl($ten_file_t);
	    move_uploaded_file($attach['tmp_name'][$i], $dirUpload . $ten_file);
	    array_push($array_t, $ten_file);
	}
	$_SESSION['ARRAY_IMG'] = $array_t;
	unset($array_t);
    }

}

?>